#!/bin/bash

# 大麦抢票助手构建脚本
# Build script for Damai Ticket Hunter

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    echo -e "${2}${1}${NC}"
}

print_success() {
    print_message "$1" "$GREEN"
}

print_error() {
    print_message "$1" "$RED"
}

print_warning() {
    print_message "$1" "$YELLOW"
}

print_info() {
    print_message "$1" "$BLUE"
}

# 检查Flutter环境
check_flutter() {
    print_info "检查Flutter环境..."
    
    if ! command -v flutter &> /dev/null; then
        print_error "Flutter未安装或未添加到PATH"
        exit 1
    fi
    
    flutter --version
    print_success "Flutter环境检查通过"
}

# 检查依赖
check_dependencies() {
    print_info "检查项目依赖..."
    
    cd mobile_app
    flutter pub get
    
    if [ $? -eq 0 ]; then
        print_success "依赖安装成功"
    else
        print_error "依赖安装失败"
        exit 1
    fi
}

# 代码分析
analyze_code() {
    print_info "执行代码分析..."
    
    flutter analyze
    
    if [ $? -eq 0 ]; then
        print_success "代码分析通过"
    else
        print_warning "代码分析发现问题，请检查"
    fi
}

# 运行测试
run_tests() {
    print_info "运行单元测试..."
    
    flutter test
    
    if [ $? -eq 0 ]; then
        print_success "所有测试通过"
    else
        print_error "测试失败"
        exit 1
    fi
}

# 构建Android APK
build_android_apk() {
    print_info "构建Android APK..."
    
    flutter build apk --release --split-per-abi
    
    if [ $? -eq 0 ]; then
        print_success "Android APK构建成功"
        print_info "APK文件位置: build/app/outputs/flutter-apk/"
        ls -la build/app/outputs/flutter-apk/
    else
        print_error "Android APK构建失败"
        exit 1
    fi
}

# 构建Android App Bundle
build_android_bundle() {
    print_info "构建Android App Bundle..."
    
    flutter build appbundle --release
    
    if [ $? -eq 0 ]; then
        print_success "Android App Bundle构建成功"
        print_info "AAB文件位置: build/app/outputs/bundle/release/"
        ls -la build/app/outputs/bundle/release/
    else
        print_error "Android App Bundle构建失败"
        exit 1
    fi
}

# 构建iOS
build_ios() {
    print_info "构建iOS应用..."
    
    if [[ "$OSTYPE" != "darwin"* ]]; then
        print_warning "iOS构建需要在macOS环境下进行"
        return
    fi
    
    flutter build ios --release --no-codesign
    
    if [ $? -eq 0 ]; then
        print_success "iOS应用构建成功"
        print_info "iOS构建文件位置: build/ios/iphoneos/"
    else
        print_error "iOS应用构建失败"
        exit 1
    fi
}

# 清理构建文件
clean_build() {
    print_info "清理构建文件..."
    
    flutter clean
    flutter pub get
    
    print_success "构建文件清理完成"
}

# 生成图标
generate_icons() {
    print_info "生成应用图标..."
    
    if flutter pub run flutter_launcher_icons:main; then
        print_success "应用图标生成成功"
    else
        print_warning "应用图标生成失败，请检查配置"
    fi
}

# 显示帮助信息
show_help() {
    echo "大麦抢票助手构建脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  check       检查环境和依赖"
    echo "  analyze     执行代码分析"
    echo "  test        运行单元测试"
    echo "  android     构建Android APK"
    echo "  bundle      构建Android App Bundle"
    echo "  ios         构建iOS应用"
    echo "  all         执行完整构建流程"
    echo "  clean       清理构建文件"
    echo "  icons       生成应用图标"
    echo "  help        显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 check           # 检查环境"
    echo "  $0 android         # 构建Android APK"
    echo "  $0 all             # 完整构建流程"
}

# 完整构建流程
build_all() {
    print_info "开始完整构建流程..."
    
    check_flutter
    check_dependencies
    analyze_code
    # run_tests  # 暂时跳过测试，因为还没有编写测试用例
    generate_icons
    build_android_apk
    build_android_bundle
    build_ios
    
    print_success "完整构建流程完成！"
}

# 主函数
main() {
    case "$1" in
        "check")
            check_flutter
            check_dependencies
            ;;
        "analyze")
            check_dependencies
            analyze_code
            ;;
        "test")
            check_dependencies
            run_tests
            ;;
        "android")
            check_dependencies
            build_android_apk
            ;;
        "bundle")
            check_dependencies
            build_android_bundle
            ;;
        "ios")
            check_dependencies
            build_ios
            ;;
        "all")
            build_all
            ;;
        "clean")
            clean_build
            ;;
        "icons")
            check_dependencies
            generate_icons
            ;;
        "help"|"--help"|"-h")
            show_help
            ;;
        "")
            print_warning "请指定操作，使用 '$0 help' 查看帮助"
            ;;
        *)
            print_error "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"